<?php

namespace Database\Seeders;

use App\Models\Admin;
use App\Models\Role;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 创建超级管理员
        $superAdmin = Admin::updateOrCreate([
            'username' => 'superadmin'
        ], [
            'email' => 'superadmin@example.com',
            'password' => Hash::make('password'), // 实际使用时应修改为安全密码
            'name' => '超级管理员',
            'phone' => '13800138000',
            'status' => 1
        ]);

        // 创建测试管理员
        $admin = Admin::updateOrCreate([
            'username' => 'admin'
        ], [
            'email' => 'admin@example.com',
            'password' => Hash::make('password'), // 实际使用时应修改为安全密码
            'name' => '管理员',
            'phone' => '13800138001',
            'status' => 1
        ]);

        // 分配角色
        $superAdminRole = Role::where('slug', 'super_admin')->first();
        $adminRole = Role::where('slug', 'admin')->first();

        if ($superAdminRole) {
            $superAdmin->roles()->sync([$superAdminRole->id]);
        }

        if ($adminRole) {
            $admin->roles()->sync([$adminRole->id]);
        }
    }
}
